﻿package
{
	import flash.display.MovieClip;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import flash.events.Event;
	
	public class DocumentClass extends MovieClip
	{
		//The canvas Bitmap is the only object in the display list
		//Everything else is "painted" to this bitmap using copypixels()
		public var canvasBitmap:Bitmap;
		//The canvas BitmapData can be thought of as containing the pixels of the Bitmap
		//The canvas Bitmap, then, is a visual representation of the canvas BitmapData
		public var canvasBitmapData:BitmapData;
		//This rectangle is used to define the region of the canvas to fill with the
		//background colour
		public var canvasRectangle:Rectangle = new Rectangle(0,0,550,400);
		//This rectangle is used to define the region of the BlockFace that should be
		//copied to the BlockFace BitmapData
		public var areaRectangle:Rectangle;
		//Each element of this array will hold a BitmapData object that is generated
		//from a different frame of the BlockFace MovieClip (one element per frame)
		public var blockFaceArray:Array;
		//This is a counter to store which frame of the animation is currently being
		//blitted to the canvas
		public var currentBlitFrame:int = 0;
		
		public function DocumentClass()
		{
			//BlockFace is just a regular animated MovieClip
			var blockFace:BlockFace;
			blockFace = new BlockFace();
			blockFace.x = 85;
			blockFace.y = 160;
			
			canvasBitmap = new Bitmap();
			canvasBitmapData = new BitmapData(550, 400);	//same dimensions as stage
			canvasBitmapData.fillRect(canvasRectangle, 0xFFDDDDDD);	//0xFFDDDDDD defines color
			canvasBitmap.bitmapData = canvasBitmapData;	//Bitmap is visualisation of raw BitmapData
			canvasBitmap.x = 0;
			canvasBitmap.y = 0;
			addChild(canvasBitmap);

			//The pixels of the BlockFace MovieClip are copied to this BitmapData,
			//and from there to the canvas BitmapData
			var blockFaceBitmapData:BitmapData;

			blockFaceArray = new Array();
			//loop through all frames of the BlockFace MovieClip and add a BitmapData drawn from the frame to the array
			for ( var i:int = 1; i <= blockFace.totalFrames; i++ )
			{
				blockFace.gotoAndStop(i);
				blockFaceBitmapData = new BitmapData(blockFace.width, blockFace.height);
				blockFaceBitmapData.draw(blockFace);
				blockFaceArray.push(blockFaceBitmapData);
			}
			blockFace.gotoAndPlay(1);
			
			//define the region of the BlockFace BitmapData to copy to the canvas later
			areaRectangle = new Rectangle();
			areaRectangle.width = blockFace.width;
			areaRectangle.height = blockFace.height;
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			blockFace = null;	//proves the BlockFace BitmapData is no longer used!
		}
		
		public function onEnterFrame(evt:Event):void
		{
			//clear the canvas every frame by filling it with the background color
			canvasBitmapData.fillRect(canvasRectangle, 0xFFDDDDDD);
			
			currentBlitFrame = currentBlitFrame + 1;
			if ( currentBlitFrame >= blockFaceArray.length )
			{
				currentBlitFrame = 0;	//make sure the code does not try to get a frame from after the end of the array
			}
			var currentBlockFaceFrame:BitmapData = blockFaceArray[currentBlitFrame];	//get the BitmapData of the current frame from the array of frames
			
			var destinationPoint:Point = new Point(85, 160);	//this is the point where the pixels of the BlockFace BitmapData will be copied to
			canvasBitmapData.copyPixels(currentBlockFaceFrame,areaRectangle,destinationPoint);	//copy the pixels from the BlockFace BitmapData to the canvas BitmapData
			//note that you don't have to tell the canvas Bitmap to re-render itself
			
			destinationPoint = new Point(85 + 10, 160 - 120);	//same as above, in a different position
			canvasBitmapData.copyPixels(currentBlockFaceFrame,areaRectangle,destinationPoint);
		
			destinationPoint = new Point(85 + 20, 160 + 120);	//same as above, in a different position
			canvasBitmapData.copyPixels(currentBlockFaceFrame,areaRectangle,destinationPoint);
		}
	}
}
